package com.dl.test.controller;

import com.alibaba.fastjson.JSON;
import com.dl.dlcommon.controller.BaseController;
import com.dl.dlcommon.utils.FileUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

/**
 * @author DL
 * @create 2022-12-08 14:47
 */
@RestController
@RequestMapping("/xml")
public class XmlController extends BaseController {

    @GetMapping("/create")
    public void create() throws ParserConfigurationException, IOException, TransformerException, SAXException {

        xmlCreate();
    }

    public void xmlCreate() throws ParserConfigurationException, IOException, SAXException, TransformerException {
        // 获取xml文件路径
        String absolutePath = FileUtil.getFileAbsolutePath("/xml/test.xml");
        System.out.println("absolutePath = " + absolutePath);
        File f = new File(absolutePath);
        // 1.创建一个DOM解析器工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // 2.通过工厂生产一个解析器对象
        DocumentBuilder builder = factory.newDocumentBuilder();
        // 3.将 xml 文件封装到 Document 对象
        Document dom = builder.parse(f);

        // 获取数据
        NodeList studentList = dom.getElementsByTagName("student");
        for (int i = 0; i < studentList.getLength(); i++) {
            Element student = (Element) studentList.item(i);
            String id = student.getAttribute("id");
            System.out.println(student.getTagName() + ":" + id);

            NodeList names = student.getElementsByTagName("name");
            NodeList ages = student.getElementsByTagName("age");
            System.out.println(names.item(0).getNodeName() + ":" + names.item(0).getTextContent());
            System.out.println(ages.item(0).getNodeName() + ":" + ages.item(0).getTextContent());
            System.out.println("=========================");
        }

        // 写入数据
        NodeList teacherList = dom.getElementsByTagName("teacher");
        System.out.println("teacherList = " + JSON.toJSONString(teacherList));
        for (int i = 0; i < teacherList.getLength(); i++) {
            Element teacher = (Element) studentList.item(i);
            Element sex = dom.createElement("sex");
            sex.setTextContent("男");
            teacher.appendChild(sex);
        }
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf = tff.newTransformer();


        // 创建 Resource 对象，指向 resource 文件夹
        Resource resourceFolder = new ClassPathResource(".");

        // 获取绝对路径
        String rootPath = resourceFolder.getFile().getAbsolutePath();
        System.out.println("rootPath: " + rootPath);

        File test2Xml = ResourceUtils.getFile(rootPath + "/xml/test2.xml");


        tf.transform(new DOMSource(dom),new StreamResult(test2Xml));
        tf.setOutputProperty(OutputKeys.INDENT,"yes");
    }

    @GetMapping("/xmlUtil")
    public void xmlUtil() throws Exception {
    }

}
